<?php

namespace App\Admin\Controllers\User;

use App\Models\CommonBank;
use App\Models\User;
use App\Models\UserRecharge;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class RechargeController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '充值记录';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new UserRecharge());

        $grid->column('id', ___('table.zy_user_recharge.Id'));
        $grid->column('user_id', ___('table.zy_user_recharge.User id'))->display(function ($id){
            $user = User::find($id);
            return $user?$user->name:'---';
        });
        $grid->column('no', ___('table.zy_user_recharge.No'));
        $grid->column('price', ___('table.zy_user_recharge.Price'));
        $grid->column('pay_name', ___('table.zy_user_recharge.Pay name'))->display(function ($id){
            $bank = CommonBank::find($id);
            if ($bank) {
                return $bank->name;
            }
        });
        $grid->column('pay_status', ___('table.zy_user_recharge.Pay status'))->display(function ($id){
            return UserRecharge::$pay_status[$id];
        });
        $grid->column('pay_time', ___('table.zy_user_recharge.Pay time'));
        $grid->column('create_time', ___('table.zy_user_recharge.Create time'));
        $grid->column('update_time', ___('table.zy_user_recharge.Update time'));
        $grid->actions(function ($actions) {

            // 去掉删除
            $actions->disableDelete();
        });
       // $grid->disableActions('');
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(UserRecharge::findOrFail($id));

        $show->field('id', ___('table.zy_user_recharge.Id'));
        $show->field('user_id', ___('table.zy_user_recharge.User id'));
        $show->field('no', ___('table.zy_user_recharge.No'));
        $show->field('price', ___('table.zy_user_recharge.Price'));
        $show->field('pay_name', ___('table.zy_user_recharge.Pay name'));
        $show->field('pay_status', ___('table.zy_user_recharge.Pay status'));
        $show->field('pay_time', ___('table.zy_user_recharge.Pay time'));
        $show->field('create_time', ___('table.zy_user_recharge.Create time'));
        $show->field('update_time', ___('table.zy_user_recharge.Update time'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new UserRecharge());

        //$form->number('user_id', ___('table.zy_user_recharge.User id'));
        $form->select('user_id' ,___('table.zy_user_recharge.User id'))->options(function ($id) {
            $user = User::find($id);
            if ($user) {
                return [$user->id => $user->name];
            }
        })->ajax('/admin/ajax/user');
        $form->text('no', ___('table.zy_user_recharge.No'));
        $form->decimal('price', ___('table.zy_user_recharge.Price'));
        $form->select('pay_name', ___('table.zy_user_recharge.Pay name'))->options(function ($id) {
            $bank = CommonBank::find($id);
            if ($bank) {
                return [$bank->id => $bank->name];
            }
        })->ajax('/admin/ajax/banklist');

        $form->radio('pay_status', ___('table.zy_user_recharge.Pay status'))->options(
            UserRecharge::$pay_status
        );

        $form->datetime('pay_time', ___('table.zy_user_recharge.Pay time'))->format('YYYY-MM-DD HH:mm:ss');
        //$form->number('create_time', ___('table.zy_user_recharge.Create time'));
        //$form->number('update_time', ___('table.zy_user_recharge.Update time'));

        return $form;
    }
}
